<?php 
	// 单文件上传
// 	$fileInfo = $_FILES['myFile'];

function uploadFile($fileInfo,$allowExt = array('jpeg','jpg','png','gif'),$maxSize = 2097152,$flag=true,$uploadPath = 'uploads'){

 	// 判断错误号
 	$msg = '';
 	if ($fileInfo['error'] > 0) {
 		switch ($fileInfo['error']) {
			case 1:
				$msg = "大小超过限制";
				break;

			case 2:
				$msg = "大小超过了html表单中MAX_FILE_SIZE选项指定值";
				break;

			case 3:
				$msg = "文件只有部分被上传";
				break;

			case 4:
				$msg = "没有文件被上传";
				break;

			case 6:
				$msg = "找不到临时文件夹";
				break;

			case 7:
				$msg = "系统错误，文件写入失败";
				break;

			case 8:
				$msg = "系统错误，上传文件被PHP扩展文件中断";
				break;
			
			default:
				break;
		}
		exit($msg);
 	}

	$ext = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
//	$allowExt = array('jpeg','jpg','png','gif');

    if (!is_array($allowExt)){
        exit('系统错误');
    }


 	// 检测上传文件的类型
	if (!in_array($ext,$allowExt)){
		exit('非法文件类型');
	}

//	$maxSize = 2097152; //2M
    //检测上传文件大小是否符合规范
	if ($fileInfo['size'] > $maxSize){
		exit('上传文件过大');
	}


	//检测文件是否是真实的图片类型
	if ($flag){
		if (!getimagesize($fileInfo['tmp_name'])){
			exit('不是真实图片类型');
		}
	}


	//检测文件是否是通过POST方式上传的
	if (!is_uploaded_file($fileInfo['tmp_name'])){
		exit('文件不是通过HTTP POST方式上传上来的');
	}

//	$uploadPath = 'uploads';

	if (!file_exists($uploadPath)){
		mkdir($uploadPath,0777,true);
		chmod($uploadPath,0777);
	}

    $uniName = md5(uniqid(microtime(true),true)) . '.' . $ext; //保证唯一性
	$destination = $uploadPath . '/' . $uniName;


	if (!@move_uploaded_file($fileInfo['tmp_name'],$destination)){
		exit('文件移动失败');
	}

//	echo '文件上传成功';
	return array(
		'newName' => $destination,
		'size'    => $fileInfo['size'],
		'type'	  => $fileInfo['type'],
	);
}